<template>
  <div class="category-nav">
    <!-- 导航头部 -->
    <div class="nav-header">
      <h1 class="site-title">我的导航</h1>
      <p class="site-desc">收藏美好，分享快乐</p>
    </div>

    <!-- 分类列表 -->
    <div class="category-list">
      <!-- 全部书签选项 -->
      <div 
        class="category-item"
        :class="{ active: !currentCategory }"
        @click="handleSelect(null)"
      >
        <i class="el-icon-collection"></i>
        <span class="category-name">全部书签</span>
        <span class="bookmark-count">{{ totalBookmarks }}</span>
      </div>

      <!-- 分类项目 -->
      <div 
        v-for="category in sortedCategories" 
        :key="category._id"
        class="category-item"
        :class="{ active: currentCategory === category._id }"
        @click="handleSelect(category._id)"
      >
        <i :class="category.icon || 'el-icon-folder'" :style="{ color: category.color }"></i>
        <span class="category-name">{{ category.name }}</span>
        <span class="bookmark-count">{{ getCategoryCount(category._id) }}</span>
      </div>
    </div>

    <!-- 底部信息 -->
    <div class="nav-footer">
      <div class="stats">
        <div class="stat-item">
          <i class="el-icon-collection"></i>
          <span>{{ totalBookmarks }} 个书签</span>
        </div>
        <div class="stat-item">
          <i class="el-icon-folder"></i>
          <span>{{ categories.length }} 个分类</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'CategoryNav',
  computed: {
    ...mapState(['categories', 'bookmarks', 'currentCategory']),
    sortedCategories() {
      return [...this.categories].sort((a, b) => {
        // 首先按照 order 排序
        if (a.order !== b.order) {
          return a.order - b.order
        }
        // 如果 order 相同，按照名称排序
        return a.name.localeCompare(b.name)
      })
    },
    totalBookmarks() {
      return this.bookmarks.length
    }
  },
  methods: {
    handleSelect(categoryId) {
      this.$store.commit('setCurrentCategory', categoryId)
    },
    getCategoryCount(categoryId) {
      return this.bookmarks.filter(b => b.category === categoryId).length
    }
  }
}
</script>

<style lang="less" scoped>
.category-nav {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: linear-gradient(to bottom, #ffffff, #f8f9fa);
  
  .nav-header {
    padding: 24px 20px;
    text-align: center;
    border-bottom: 1px solid #ebeef5;
    
    .site-title {
      margin: 0;
      font-size: 20px;
      font-weight: 600;
      color: #303133;
      letter-spacing: 1px;
    }
    
    .site-desc {
      margin: 8px 0 0;
      font-size: 13px;
      color: #909399;
    }
  }
  
  .category-list {
    flex: 1;
    padding: 16px 0;
    overflow-y: auto;
    
    &::-webkit-scrollbar {
      width: 6px;
    }
    
    &::-webkit-scrollbar-thumb {
      background-color: #e4e7ed;
      border-radius: 3px;
    }
    
    &::-webkit-scrollbar-track {
      background-color: transparent;
    }
    
    .category-item {
      position: relative;
      display: flex;
      align-items: center;
      padding: 12px 20px;
      cursor: pointer;
      transition: all 0.3s ease;
      
      i {
        font-size: 16px;
        margin-right: 12px;
        color: #909399;
        transition: all 0.3s ease;
      }
      
      .category-name {
        flex: 1;
        font-size: 14px;
        color: #606266;
        transition: all 0.3s ease;
      }
      
      .bookmark-count {
        font-size: 12px;
        color: #909399;
        background-color: #f4f4f5;
        padding: 2px 6px;
        border-radius: 10px;
        transition: all 0.3s ease;
      }
      
      &:hover {
        background-color: #f5f7fa;
        
        .category-name {
          color: #409EFF;
        }
        
        i {
          transform: scale(1.1);
        }
      }
      
      &.active {
        background-color: #ecf5ff;
        
        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          bottom: 0;
          width: 3px;
          background-color: #409EFF;
        }
        
        .category-name {
          color: #409EFF;
          font-weight: 500;
        }
        
        .bookmark-count {
          background-color: #409EFF;
          color: #fff;
        }
      }
    }
  }
  
  .nav-footer {
    padding: 16px 20px;
    border-top: 1px solid #ebeef5;
    
    .stats {
      display: flex;
      flex-direction: column;
      gap: 8px;
      
      .stat-item {
        display: flex;
        align-items: center;
        color: #909399;
        font-size: 13px;
        
        i {
          margin-right: 8px;
          font-size: 14px;
        }
      }
    }
  }
}

// 响应式设计
@media screen and (max-width: 768px) {
  .category-nav {
    .nav-header {
      padding: 16px;
      
      .site-title {
        font-size: 18px;
      }
    }
    
    .category-list {
      padding: 8px 0;
      
      .category-item {
        padding: 10px 16px;
      }
    }
    
    .nav-footer {
      display: none; // 在移动端隐藏底部统计信息
    }
  }
}
</style> 